package com.example.handler.security;

import com.example.domain.ResponseResult;
import com.example.domain.enums.AppHttpCodeEnum;
import com.example.util.JsonUtil;
import com.example.util.WebUtil;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 *  授权失败失败类
 *
 * @author hlc
 * @version 1.0
 * @date 2022/10/13 20:21
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        accessDeniedException.printStackTrace();
        ResponseResult<?> result = ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        String json = JsonUtil.obj2Str(result);
        // 处理异常
        WebUtil.renderString(response, json);
    }
}
